<template>
  <work-template v-model="detailForm" @submitOn="submitOn" :flowId="flowId" @closeSelectedTag="closeSelectedTag"
                 :butShow="bool">
    <div slot="form" class="container-div">
      <vehicle-basice-info v-model="detailForm" :state="isEdit" ref="refVehicleBasiceInfo"/>

      <upkeep-basce-info v-model="detailForm" ref="refUpkeepDetailForm" :state="isEdit"/>

      <maintenance-ttem v-model="detailForm" ref="refMaintenanceTtem" :state="isEdit"/>
    </div>
  </work-template>
</template>

<script>
import {getToken} from "@/utils/auth";
import {
  updateMaintain,
  addMaintain,
  getMaintain,
} from "@/api/cwgd/maintainApi.js";
import {getFindCarWorkOrderInfoVo} from "@/api/cwgd/maintenanceApi.js";
import LinkageSelect from "../../components/linkageSelect";
import UploadFile from "../../../components/UploadFile";
import vehicleBasiceInfo from "../components/upkeep/vehicleBasiceInfo.vue";
import upkeepBasceInfo from "../components/upkeep/upkeepBasceInfo.vue";
import maintenanceTtem from "../components/upkeep/maintenanceTtem.vue";
import upkeepFeedback from "../components/upkeep/upkeepFeedback.vue";

export default {
  dicts: ["work_order_type", "online_offline", "maintain_type"],
  name: "MaintainDetails",
  components: {
    LinkageSelect,
    UploadFile,
    vehicleBasiceInfo,
    upkeepBasceInfo,
    maintenanceTtem,
    upkeepFeedback,
  },

  data() {
    return {
      isForm: true,
      method: {},
      // 表单参数
      detailForm: {
        vinNo: "",
        carNo: "",
        brandName: "",
        seriesName: "",
        modelName: "",
        belongCityName: "",
        orderNo: "",
        leaseNum: "",
        customerName: "",
        leaseStartTime: "",
        leaseEndTime: "",
        totalBudgetPrice: "",

        registerTime: "",
        bicycleBudgetPrice: "",
        replacePrice: "",
        carSurplusPrice: "",

        workId: "",
        travelMeligile: "",
        businessDataId: "",
        expectStartTime: "",
        expectEndTime: "",
        maintainShop: "",
        maintainShopId: '',
        faultDescribe: "",
        maintainQuoter: "",
        reasonForClaim: "",
        carId: "",
        claimantReason: "",
        swappingAccessories: "",
        comprehensiveRecommendations: "",
        maintainRemark: "",
        licensePlateTime: "",
        carMaintainProjectList: [],
        maintainProjectUrl: "",

        //资金部反馈
        actualStartTime: "",
        actualEndTime: "",
        feedbackAdvancePayment: "",
        payType: "",
        maintainCompleteUrl: "",
        balanceUrl: "",
        maintainFeedback: "",
      },

      addAmountNun: 0,
    };
  },
  watch: {},
  computed: {
    isEdit() {
      if (this.$route.query.type === "edit") {
        return "write";
      }
      if (this.$route.query.type === "view") {
        return "readonly";
      }
    },
    bool() {
      if (this.$route.query.type === "edit") {
        return true;
      }
      if (this.$route.query.type === "view") {
        return false;
      }
    },
    flowId() {
      if (this.$route.query.flowId) {
        return this.$route.query.flowId === '{flowId}' ? '' : this.$route.query.flowId
      }
    },
  },
  created() {
    this.method = this.$route.query;
    this.detailForm.carId = this.method.carId;
    this.detailForm.workId = this.method.id
    this.handleGetFindCarWorkOrderInfoVo()
    if (this.$route.query.businessDataId && this.$route.query.businessDataId !== 'null') {
      this.handleGetMaintain();
    }
  },
  mounted() {
  },
  methods: {
    closeSelectedTag(view) {
      this.$tab.closePage(view).then(({visitedViews}) => {
        // this.toLastView(visitedViews, view)
        this.$router.push({
          path: "/cwgd/maintainList",
        });
      });
    },

    handleGetFindCarWorkOrderInfoVo() {
      getFindCarWorkOrderInfoVo(this.$route.query.id).then(res => {
        if (res.code === 200) {
          for (let key in res.data) {
            this.$set(this.detailForm, key, res.data[key]);
          }
          this.detailForm.maintainQuoter = res.data.maintainQuoter || 0
          this.detailForm.workId = res.data?.workId || this.$route.query.id
        }
      })
    },

    // 提交操作
    submitOn() {
      let refList = [
        this.$refs.refVehicleBasiceInfo.handleEffect(),
        this.$refs.refUpkeepDetailForm.handleEffect(),
        this.$refs.refMaintenanceTtem.handleEffect(),
      ];

      Promise.all(refList)
          .then(() => {
            const loading = this.$loading({
              lock: true,
              text: "保存中",
              spinner: "el-icon-loading",
              background: "rgba(0, 0, 0, 0.7)",
            });
            addMaintain(this.detailForm)
                .then((res) => {
                  if (res.code === 200) {
                    loading.close();
                    this.closeSelectedTag(this.$route);
                  }
                })
                .catch(() => {
                  loading.close();
                });
          })
          .catch(() => {
          });
    },

    // 详情回显
    handleGetMaintain() {
      let id = this.method.businessDataId;

      getMaintain(id).then((res) => {
        if (res.code === 200) {
          this.isForm = true;
          this.detailForm = res.data
          this.detailForm.workId = this.$route.query.id
        }
      });
    },
  },
};
</script>


<style lang="scss" scoped>
@import "@/styles/cwgd/container.scss";
</style>
